// pages/map/map.js
const types = ['default', 'primary', 'warn']
const pageObject = {
  data: {
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false
  },

  onShareAppMessage() {
    return {
      title: 'button',
      path: 'page/component/pages/button/button'
    }
  },

  setDisabled() {
    this.setData({
      disabled: !this.data.disabled
    })
  },

  setPlain() {
    this.setData({
      plain: !this.data.plain
    })
  },

  setLoading() {
    this.setData({
      loading: !this.data.loading
    })
  },
  
  handleContact(e) {
    console.log(e.detail)
  },

  handleGetPhoneNumber(e) {
    console.log(e.detail)
  },

  handleGetUserInfo(e) {
    console.log(e.detail)
  },

  handleOpenSetting(e) {
    console.log(e.detail.authSetting)
  },

  handleGetUserInfo(e) {
    console.log(e.detail.userInfo)
  }
}

for (let i = 0; i < types.length; ++i) {
  (function (type) {
    pageObject[type] = function () {
      const key = type + 'Size'
      const changedData = {}
      changedData[key] =
        this.data[key] === 'default' ? 'mini' : 'default'
      this.setData(changedData)
    }
  }(types[i]))
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        latitude: 23.099994,
        longitude: 113.324520,
        subKey: '55RBZ-INZWU-OJRVI-2BOG4-WAIKF-AUBRA',
        enable3d: false,
        showLocation: true,
        showCompass: false,
        enableOverlooking: false,
        enableZoom: true,
        enableScroll: true,
        enableRotate: false,
        drawPolygon: false,
        enableSatellite: false,
        enableTraffic: false,
        markers: [],
        dialog: false,
        marker: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.getLocation({
            type: 'gcj02',
            success: (res) => {
              const latitude = res.latitude;
              const longitude = res.longitude;
              const map = wx.createMapContext('map');
              map.moveToLocation({
                latitude: latitude,
                longitude: longitude
              })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.request({
          url: 'https://car.ispongecity.com/lesson/features/layer/充电',
          success: (res) => {
              const markers = res.data.map((item,index) => {
                  return {
                      id: index,
                    latitude: item.geometry.coordinates[1],
                    longitude: item.geometry.coordinates[0],
                    width: 20,
                    height: 20,
                    name: item.properties["名称"],
                    label: {
                        content: item.properties["名称"],
                        color: '#ff0000'
                    }
                  }
              });
              console.log(markers);
              this.setData({
                markers: markers
              });
          }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    markertap(e) {
        const marker = this.data.markers.find(item => item.id == e.detail.markerId);
        this.setData({
            dialog: true,
            marker: marker
        });
    },

    navi1() {
        wx.openLocation({
            latitude: this.data.marker.latitude,
            longitude: this.data.marker.longitude,
            scale: 18
        });
    },

    navi2() {
        let plugin = requirePlugin('routePlan');
        let key = '55RBZ-INZWU-OJRVI-2BOG4-WAIKF-AUBRA';  //使用在腾讯位置服务申请的key
        let referer = 'wxd9f7fd7fad5a63ac';   //调用插件的app的名称
        let endPoint = JSON.stringify({  //终点
            'name': this.data.marker.name,
            'latitude': this.data.marker.latitude,
            'longitude': this.data.marker.longitude
        });
        wx.navigateTo({
            url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
        });

    }
})